/*
 * 行车数据分析报表-旧版
 * */
require(['date', 'validator', 'highcharts', 'highcharts-more'], function () {

    window.show = function (arr) {
        //接收传参
        var data = arr[0];
        //console.log(data);

        loadRecordAnalysisData(data.carId, data.beginTime, data.endTime);
    };

    function loadRecordAnalysisData(carId, beginTime, endTime) {
        $.ajaxExt({
            url: "/carAna/getRecordAnalysisChart.htm",
            data: {'carId': carId, 'beginTime': beginTime, 'endTime': endTime}
        }).done(function (result) {
            var spdOfMotVhiArr = [];
            var engiRtteSpdArr = [];
            var instFuelCsutArr = [];
            var acltPedalOpeArr = [];
            var electThroOpeArr = [];
            var oxygenConcenArr = [];
            var engiAirAdmissionTeptArr = [];
            var airAdmissionPrsArr = [];
            var engiWteTpteArr = [];
            var engiEngnOilPrsArr = [];
            var preAirPrsArr = [];
            var lastAirPrsArr = [];
            $(result).each(function (i, item) {
                spdOfMotVhiArr.push([item.recordTime, item.spdOfMotVhi == 0 ? item.spdOfMotVhi : item.spdOfMotVhi || null]);
                engiRtteSpdArr.push([item.recordTime, item.engiRtteSpd == 0 ? item.engiRtteSpd : item.engiRtteSpd || null]);
                acltPedalOpeArr.push([item.recordTime, item.acltPedalOpe == 0 ? item.acltPedalOpe : item.acltPedalOpe || null]);
                electThroOpeArr.push([item.recordTime, item.electThroOpe == 0 ? item.electThroOpe : item.electThroOpe || null]);
                oxygenConcenArr.push([item.recordTime, item.oxygenConcen == 0 ? item.oxygenConcen : item.oxygenConcen || null]);
                engiAirAdmissionTeptArr.push([item.recordTime, item.engiAirAdmissionTept == 0 ? item.engiAirAdmissionTept : item.engiAirAdmissionTept || null]);
                airAdmissionPrsArr.push([item.recordTime, item.airAdmissionPrs == 0 ? item.airAdmissionPrs : item.airAdmissionPrs || null]);
                instFuelCsutArr.push([item.recordTime, item.instFuelCsut == 0 ? item.instFuelCsut : item.instFuelCsut || null]);
                engiWteTpteArr.push([item.recordTime, item.engiWteTpte == 0 ? item.engiWteTpte : item.engiWteTpte || null]);
                engiEngnOilPrsArr.push([item.recordTime, item.engiEngnOilPrs * 1000 == 0 ? item.engiEngnOilPrs * 1000 : item.engiEngnOilPrs * 1000 || null]);
                preAirPrsArr.push([item.recordTime, item.preAirPrs * 1000 == 0 ? item.preAirPrs * 1000 : item.preAirPrs * 1000 || null]);
                lastAirPrsArr.push([item.recordTime, item.lastAirPrs * 1000 == 0 ? item.lastAirPrs * 1000 : item.lastAirPrs * 1000 || null]);
            });
            createRecordChart(spdOfMotVhiArr, engiRtteSpdArr, acltPedalOpeArr, electThroOpeArr, oxygenConcenArr,
                engiAirAdmissionTeptArr, airAdmissionPrsArr, instFuelCsutArr, engiWteTpteArr, engiEngnOilPrsArr,
                preAirPrsArr, lastAirPrsArr);
        });
    };

    function createRecordChart(spdOfMotVhiArr, engiRtteSpdArr, acltPedalOpeArr, electThroOpeArr, oxygenConcenArr,
                               engiAirAdmissionTeptArr, airAdmissionPrsArr, instFuelCsutArr, engiWteTpteArr, engiEngnOilPrsArr,
                               preAirPrsArr, lastAirPrsArr) {
        var names = ['瞬时油耗', '油门踏板开度', '电子节气门开度', '氧浓度', '进气温度', '进气压力', '车速', '转速', '机油压力', '水温', '前气压', '后气压'];
        var colors = ['#2c82c9', '#EEE657', '#FCB941', '#FC6042', '#3BC5ED', '#D87737', '#53DC60', '#8771CF', '#E25979', '#35B6CD', '#203079', '#E2DD30'];

        $('#recordAnalysisChart').highcharts({
            chart: {
                type: 'spline',
                zoomType: 'x',
                plotBorderWidth: 1,
                marginTop: 38,
                marginLeft: 90,
                marginRight: 180,
                height: $(document).height() - 120,
                width: $(document).width() - 50
            },
            title: {
                text: ''
            },
            credits: {
                enabled: false
            },
            tooltip: {
                shared: true,
                formatter: function () {
                    var html = '时间：' + new Date(this.x - 1000 * 60 * 60 * 8).format('yyyy-MM-dd hh:mm:ss') + '<br>';
                    $.each(this.points, function (index, item) {
                        if (item.series.name == names[0]) {
                            html += names[0] + ' :' + item.y + ' L/H<br>';
                        }
                        if (item.series.name == names[1]) {
                            html += names[1] + ' :' + item.y + ' %<br>';
                        }
                        if (item.series.name == names[2]) {
                            html += names[2] + ' :' + item.y + ' %<br>';
                        }
                        if (item.series.name == names[3]) {
                            html += names[3] + ' :' + item.y + ' %<br>';
                        }
                        if (item.series.name == names[4]) {
                            html += names[4] + ' :' + item.y + ' ℃<br>';
                        }
                        if (item.series.name == names[5]) {
                            html += names[5] + ' :' + item.y + ' mpa<br>';
                        }
                        if (item.series.name == names[6]) {
                            html += names[6] + ' :' + item.y + ' Km/h<br>';
                        }
                        if (item.series.name == names[7]) {
                            html += names[7] + ' :' + item.y + ' r/min<br>';
                        }
                        if (item.series.name == names[8]) {
                            html += names[8] + ' :' + item.y / 1000 + ' mpa<br>';
                        }
                        if (item.series.name == names[9]) {
                            html += names[9] + ':' + item.y + ' ℃<br>';
                        }
                        if (item.series.name == names[10]) {
                            html += names[10] + ' :' + item.y / 1000 + ' mpa<br>';
                        }
                        if (item.series.name == names[11]) {
                            html += names[11] + ' :' + item.y / 1000 + ' mpa';
                        }
                    });

                    return html;
                },
                crosshairs: true
            },
            plotOptions: {
                spline: {
                    lineWidth: 2/*,
                     states: {
                     hover: {
                     lineWidth: 5
                     }
                     }*/
                }
            },
            xAxis: {
                tickmarkPlacement: 'on',
                type: 'datetime'
            },
            yAxis: [
                {
                    min: 0,
                    title: {
                        text: 'L/H',
                        offset: 30,
                        rotation: 0,
                        y: -10,
                        align: 'high'
                    },
                    opposite: true,
                    max: 50
                },
                {
                    min: -100,
                    title: {
                        text: '%',
                        offset: 30,
                        rotation: 0,
                        y: -10,
                        align: 'high'
                    },
                    opposite: true,
                    max: 100
                },
                {
                    min: 0,
                    title: {
                        text: '℃',
                        offset: 30,
                        rotation: 0,
                        y: -10,
                        align: 'high'
                    },
                    opposite: true,
                    max: 100
                },
                {
                    min: 0,
                    title: {
                        text: 'mpa',
                        offset: 30,
                        rotation: 0,
                        y: -10,
                        align: 'high'
                    },
                    opposite: true,
                    max: 2000
                },
                {
                    min: 0,
                    title: {
                        text: 'Km/h',
                        offset: 0,
                        rotation: 0,
                        y: -10,
                        align: 'high'
                    },
                    max: 120
                },
                {
                    min: 0,
                    title: {
                        text: 'r/min',
                        offset: 10,
                        rotation: 0,
                        y: -10,
                        align: 'high'
                    }
                }
            ],
            series: [
                {
                    data: instFuelCsutArr,
                    name: names[0],
                    color: colors[0],
                    marker: {
                        enabled: false
                    }
                },
                {
                    data: acltPedalOpeArr,
                    name: names[1],
                    color: colors[1],
                    yAxis: 1,
                    marker: {
                        enabled: false
                    }
                },
                {
                    data: electThroOpeArr,
                    name: names[2],
                    color: colors[2],
                    yAxis: 1,
                    marker: {
                        enabled: false
                    }
                },
                {
                    data: oxygenConcenArr,
                    name: names[3],
                    color: colors[3],
                    yAxis: 1,
                    marker: {
                        enabled: false
                    }
                },
                {
                    data: engiAirAdmissionTeptArr,
                    name: names[4],
                    color: colors[4],
                    yAxis: 2,
                    marker: {
                        enabled: false
                    }
                },
                {
                    data: airAdmissionPrsArr,
                    name: names[5],
                    color: colors[5],
                    yAxis: 3,
                    marker: {
                        enabled: false
                    }
                },
                {
                    data: spdOfMotVhiArr,
                    name: names[6],
                    color: colors[6],
                    yAxis: 4,
                    marker: {
                        enabled: false
                    }
                },
                {
                    data: engiRtteSpdArr,
                    name: names[7],
                    color: colors[7],
                    yAxis: 5,
                    marker: {
                        enabled: false
                    }
                },
                {
                    data: engiEngnOilPrsArr,
                    name: names[8],
                    color: colors[8],
                    yAxis: 3,
                    marker: {
                        enabled: false
                    }
                },
                {
                    data: engiWteTpteArr,
                    name: names[9],
                    color: colors[9],
                    yAxis: 2,
                    marker: {
                        enabled: false
                    }
                },
                {
                    data: preAirPrsArr,
                    name: names[10],
                    color: colors[10],
                    yAxis: 3,
                    marker: {
                        enabled: false
                    }
                },
                {
                    data: lastAirPrsArr,
                    name: names[11],
                    color: colors[11],
                    yAxis: 3,
                    marker: {
                        enabled: false
                    }
                }
            ]

        });
    }
});